package com.wjj.happytime.adapter.happy_frg_adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import android.widget.Toast;
import com.jiongbull.jlog.JLog;
import com.squareup.picasso.Picasso;
import com.wjj.happytime.R;
import com.wjj.happytime.application.BaseApplication;
import com.wjj.happytime.entity.happy_entity.PerItem;
import com.wjj.happytime.util.CircleTransformation;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * RecyclerView适配器
 * created by Wang_JunJie at 2017/1/13
 */
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {

  private Context context;
  private List<PerItem> list;
  public static final String IMAGE_URL = "http://pic.qiushibaike.com/system/pictures/%s/%s/medium/%s";
  public static final String ICON_URL = "http://pic.qiushibaike.com/system/avtnew/%d/%d/thumb/%s";
  public ItemViewHolder itemViewHolder;


  public ItemAdapter(Context context) {
    this.context = context;
    list = new LinkedList<>();
  }

  @Override
  public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    itemViewHolder = new ItemViewHolder(
        LayoutInflater.from(context).inflate(R.layout.common_item, parent, false));
    return itemViewHolder;
  }

  @Override
  public void onBindViewHolder(ItemViewHolder holder, int position) {
    //判断用户是否存在 存在就显示 否则就隐藏
    final PerItem item = list.get(position);
    if (item.getUser() != null) {
      holder.userInfo.setVisibility(View.VISIBLE);
      holder.userName.setText(item.getUser().getLogin());
//            User user = item.getUser();
//            String url = String.format(Locale.getDefault(), ICON_URL, user.getId() / 10000, user.getId(), user.getIcon());
      String thumbUrl = "http:" + item.getUser().getThumb();
      String mediumUrl = "http:" + item.getUser().getMedium();
      Picasso.with(context).load(thumbUrl).transform(new CircleTransformation())
          .into(holder.userIcon);
    } else {
      holder.userInfo.setVisibility(View.GONE);
    }

    if ("video".equals(item.getFormat())) {
      //JC播放器
      holder.jcvpItem.setVisibility(View.VISIBLE);
      holder.jcvpItem
          .setUp(item.getLow_url(), JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, item.getContent());
      Picasso.with(context).load(item.getPic_url()).into(holder.jcvpItem.thumbImageView);
      holder.itemImage.setVisibility(View.GONE);
    } else {
      holder.jcvpItem.setVisibility(View.GONE);
    }
    //判断图片是否存在
    if ("image".equals(item.getFormat())) {
      holder.itemImage.setVisibility(View.VISIBLE);
//                String image = item.getImage();
//                Pattern pattern = Pattern.compile("(\\d+)\\d{4}");
//                Matcher matcher = pattern.matcher(image);
//                if (matcher.find()) {
//                   final String url = String.format(Locale.getDefault(), IMAGE_URL, matcher.group(1), matcher.group(), image);
      final String lowUrl = "http:" + item.getLow_loc();
      final String highUrl = "http:" + item.getHigh_loc();
      Picasso.with(context).load(lowUrl).into(holder.itemImage);
      JLog.e(highUrl);
      holder.itemImage.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
          final Dialog dialog = new Dialog(context, R.style.Dialog_Fullscreen);
          dialog.setContentView(R.layout.dialog_item_image);
          ImageView image = (ImageView) dialog.findViewById(R.id.iv_dialog_image);
          dialog.findViewById(R.id.rl_dialog).setOnClickListener(
              new OnClickListener() {
                @Override
                public void onClick(View view) {
                  dialog.dismiss();
                }
              });
          Picasso.with(context).load(highUrl).into(image);
          dialog.show();

        }
      });
//                }

    } else {
      holder.itemImage.setVisibility(View.GONE);
    }

    holder.itemContent.setText(item.getContent());
    holder.cvItem.setOnLongClickListener(new OnLongClickListener() {
      @Override
      public boolean onLongClick(View view) {
//                +"format text,"
//                    + " thumb text,"
//                    + "name text,"
//                    + "content text,"
//                    + "low_loc text,"
//                    + "high_loc text,"
//                    + "low_url text,"
//                    + "pic_url text)");
        SQLiteDatabase db = ((BaseApplication) ((Activity) context).getApplication()).db;
        db.execSQL(
            "INSERT INTO tableHappy(format,thumb,name,content,low_loc,high_loc,low_url,pic_url) VALUES(?,?,?,?,?,?,?,?)",
            new String[]{item.getFormat(), item.getUser().getThumb(),
                item.getUser().getLogin(), item.getContent(),
                item.getLow_loc(), item.getHigh_loc(),
                item.getLow_url(), item.getPic_url()});
        Toast.makeText(context, "加入收藏夹成功！", Toast.LENGTH_SHORT).show();
        return false;
      }
    });
  }

  public void addAll(Collection<? extends PerItem> collections) {
    int size = list.size();
    list.addAll(collections);
    notifyItemRangeChanged(size, collections.size());
  }

  public void cleanData() {
    if (list.size() != 0) {
      list.clear();
      notifyDataSetChanged();
    }
  }

  @Override
  public int getItemCount() {
    return list.size();
  }

  public class ItemViewHolder extends RecyclerView.ViewHolder {

    private final ImageView userIcon;
    private final TextView userName;
    private final TextView itemContent;
    private final ImageView itemImage;
    private final LinearLayout userInfo;
    public ImageView ivPlay;
    public JCVideoPlayerStandard jcvpItem;
    public CardView cvItem;

    public ItemViewHolder(View itemView) {
      super(itemView);
      userIcon = (ImageView) itemView.findViewById(R.id.iv_user_icon);
      userName = (TextView) itemView.findViewById(R.id.tv_user_name);
      userInfo = ((LinearLayout) itemView.findViewById(R.id.ll_user_info));
      itemContent = ((TextView) itemView.findViewById(R.id.tv_item_content));
      itemImage = ((ImageView) itemView.findViewById(R.id.tv_item_image));
      ivPlay = ((ImageView) itemView.findViewById(R.id.iv_play));
      jcvpItem = ((JCVideoPlayerStandard) itemView.findViewById(R.id.jcvp_item));
      cvItem = ((CardView) itemView.findViewById(R.id.cv_common_item));

    }
  }

  public void releaseAllVideos() {
    itemViewHolder.jcvpItem.releaseAllVideos();

//        if (itemViewHolder.jcvpItem.startButton.performClick()) {
//            itemViewHolder.jcvpItem.release();
//        }

  }

}
